package com.hardson.toolkit.swing.table;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class DTable extends JTable {
	private static final long serialVersionUID = 1L;

	private GridSplit gridSplit;

	public static DTable create(final TableContent tableContent, final Object[] columnHeaders,
			final List<GroupHeader> goupColumnHeaderList) {
		DTable table = tableContent.createTable(columnHeaders);

		table.addGroupColumnHeaderList(goupColumnHeaderList);
		return table;
	}

	public DTable(final TableModel tableModel) {
		super(tableModel);
	}

	public DTable(final GridSplit gridSplit, final TableModel tbl) {
		super(tbl);
		this.gridSplit = gridSplit;
		this.setUI(new DTableUI());// 设置Jtable的渲染UI
	}

	@Override
	protected JTableHeader createDefaultTableHeader() {
		return new GroupableTableHeader(this.columnModel);
	}

	public void addGroupColumnHeaderList(final List<GroupHeader> goupColumnHeaderList) {
		GroupableTableHeader header = (GroupableTableHeader) this.getTableHeader();
		TableColumnModel tableColumnModel = this.getColumnModel();
		for (GroupHeader groupHeader : goupColumnHeaderList) {
			header.addColumnGroup(groupHeader.createColumnGroup(tableColumnModel));
		}
		for (int i = 0; i < tableColumnModel.getColumnCount(); i++) {
			tableColumnModel.getColumn(i).setHeaderRenderer(new GroupRenderer());
		}
		this.getTableHeader().setUI(new GroupableTableHeaderUI(this.getTableHeader()));
	}

	@Override
	public Rectangle getCellRect(final int row, final int column, final boolean includeSpacing) {
		if (this.gridSplit == null) {
			return super.getCellRect(row, column, includeSpacing);
		}

		int colCell = this.gridSplit.visibleColCell(row, column);// 指定单元格的可视单元格列值
		int rowCell = this.gridSplit.visibleRowCell(row, column);// 指定单元格的可视单元格行值

		Rectangle rec = super.getCellRect(rowCell, colCell, includeSpacing);

		// 如果指定单元格列宽不为1，累计出跨列单元格的宽度
		for (int i = 1; i < this.gridSplit.spanCol(rowCell, colCell); i++) {
			rec.width += this.getColumnModel().getColumn(colCell + i).getWidth();
		}
		// 如果指定单元格行宽不为1，累计出跨行单元格的宽度
		for (int i = 1; i < this.gridSplit.spanRow(rowCell, colCell); i++) {
			rec.height += this.getRowHeight(rowCell + i);
		}
		return rec;
	}

	@Override
	public int columnAtPoint(final Point p) {
		int y = super.columnAtPoint(p);
		if (this.gridSplit == null) {
			return y;
		}
		// 当指定位置不在Table内时，返回－1
		if (y < 0) {
			return y;
		}
		int x = super.rowAtPoint(p);
		// 获取指定位置可视单元格的列值
		return this.gridSplit.visibleColCell(x, y);

	}

	@Override
	public int rowAtPoint(final Point p) {
		int x = super.rowAtPoint(p);
		if (this.gridSplit == null) {
			return x;
		}
		if (x < 0) {
			return x;
		}
		int y = super.columnAtPoint(p);
		return this.gridSplit.visibleRowCell(x, y);
	}

	//	public boolean isCellEditable(int row, int col) {
	//		return false;
	//	}

	@Override
	public boolean isCellSelected(final int row, final int col) {
		return this.getSelectedColumn() == col && this.getSelectedRow() == row;
	}

	//	public boolean isRowSelected(int row){
	//		this.getSelectedColumn()
	//		return false;
	//	}
	public GridSplit getGridSplit() {
		return this.gridSplit;
	}
}

//@SuppressWarnings("unchecked")
//public Class getColumnClass(int i){
////	Class clazz=clazzMap.get(i);
////	if(clazz!=null){
////		return clazz;
////	}else if(i==0&&Const.TABLE_SELECT.equals(getColumnName(0))){
////		return Boolean.class;
////	}else{
////		return String.class;
////	}
//	//this.isCellEditable(arg0, arg1)
//	return String.class;
//}
//public boolean isCellEditable(int row,int col){
//	return true;
//}
//public boolean isCellSelected(int row,int col){
//	return false;
//}
//public Object getValueAt(int row,int column){
//	int colCell = gridSplit.visibleColCell(row, column);// 指定单元格的可视单元格列值
//	int rowCell = gridSplit.visibleRowCell(row, column);// 指定单元格的可视单元格行值
//
//	return super.getValueAt(rowCell,colCell);
//}
//public TableCellEditor getCellEditor(int row,int column){
//	int rowCell = gridSplit.visibleRowCell(row, column);// 指定单元格的可视单元格行值
//	int colCell = gridSplit.visibleColCell(row, column);// 指定单元格的可视单元格列值
//	//this.getCellRenderer(row, column)
//	return super.getCellEditor(rowCell, colCell);
//
//}
//public TableCellRenderer getCellRenderer(int row,int column){
//	int rowCell = gridSplit.visibleRowCell(row, column);// 指定单元格的可视单元格行值
//	int colCell = gridSplit.visibleColCell(row, column);// 指定单元格的可视单元格列值
//
//	return super.getCellRenderer(rowCell, colCell);
//}
//this.setDefaultRenderer(String.class, new TableCellRenderer() {
//	public Component getTableCellRendererComponent(JTable table,
//			Object value, boolean isSelected, boolean hasFocus,
//			int row, int column) {
//		String text = (value == null) ? "" : value.toString();
//		JLabel cell = new JLabel(text);
//		cell.setOpaque(true);
//		if (row == 0) {
//			// cell.setForeground(headerForeground);
//			// cell.setBackground(headerBackground);
//		} else {
//			if (isSelected) {
//				// cell.setForeground(selectedForeground);
//				// cell.setBackground(selectedBackground);
//			} else {
//				// cell.setForeground(foreground);
//				// cell.setBackground(background);
//			}
//		}
//		return cell;
//	}
//});